package com.cn.zpark;

import com.alibaba.fastjson.JSON;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @PackageName: com.cn.zpark
 * @ClassName: JavaBeanToJson02
 * @Description:
 * @author: RZS
 * @date: 2022/4/29  19:41
 */
public class JavaBeanToJson02 {
    public static void main(String[] args) {
        // 创建emp对象
        Emp emp = new Emp();
        emp.setName("复杂的JavaBean");
        emp.setArray(new int[]{1, 2, 3, 4, 5});
        emp.setUser(new User(1111, "大锤", "99999"));

        // 定义；list集合
        List<User> list = new ArrayList<>();
        list.add(new User(1, "张三", "123456"));
        list.add(new User(2, "大锤", "123456"));
        list.add(new User(3, "王二狗", "123456"));

        emp.setList(list);

        // 定义map
        Map<String, Object> map = new HashMap<>();
        // 添加内容
        map.put("name", "张三");
        map.put("date", new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
        map.put("array", new int[]{1, 2, 3, 4});

        emp.setMap(map);

        // 创建set集合
        Set<String> set = new HashSet<>();
        set.add("java");
        set.add("Python");

        emp.setHashSet(set);

        // 将复杂的JavaBean转换为json字符串
        String jsonString = JSON.toJSON(emp).toString();
        System.out.println(jsonString);
    }
}
